package com.rmy.queue.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rmy.CompUtils;
import com.rmy.domain.CompArea;
import com.rmy.queue.domain.QmsCompConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rmy.common.annotation.Log;
import com.rmy.common.core.controller.BaseController;
import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.enums.BusinessType;
import com.rmy.queue.domain.QmsBerrConfig;
import com.rmy.queue.service.IQmsBerrConfigService;
import com.rmy.common.utils.poi.ExcelUtil;
import com.rmy.common.core.page.TableDataInfo;
import rmy.BerrUtils;
import rmy.domain.BerrArea;

/**
 * 鹤位屏配置Controller
 *
 * @author rmy
 * @date 2022-10-06
 */
@RestController
@RequestMapping("/queue/berrconfig")
public class QmsBerrConfigController extends BaseController
{
    @Autowired
    private IQmsBerrConfigService qmsBerrConfigService;

    /**
     * 查询鹤位屏配置列表
     */
    @PreAuthorize("@ss.hasPermi('queue:berrconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(QmsBerrConfig qmsBerrConfig)
    {
        startPage();
        List<QmsBerrConfig> list = qmsBerrConfigService.selectQmsBerrConfigList(qmsBerrConfig);
        return getDataTable(list);
    }

    /**
     * 导出鹤位屏配置列表
     */
    @PreAuthorize("@ss.hasPermi('queue:berrconfig:export')")
    @Log(title = "鹤位屏配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsBerrConfig qmsBerrConfig)
    {
        List<QmsBerrConfig> list = qmsBerrConfigService.selectQmsBerrConfigList(qmsBerrConfig);
        ExcelUtil<QmsBerrConfig> util = new ExcelUtil<QmsBerrConfig>(QmsBerrConfig.class);
        util.exportExcel(response, list, "鹤位屏配置数据");
    }

    /**
     * 获取鹤位屏配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('queue:berrconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qmsBerrConfigService.selectQmsBerrConfigById(id));
    }

    /**
     * 新增鹤位屏配置
     */
    @PreAuthorize("@ss.hasPermi('queue:berrconfig:add')")
    @Log(title = "鹤位屏配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsBerrConfig qmsBerrConfig)
    {
        qmsBerrConfig.setCreateBy(getUsername());
        return toAjax(qmsBerrConfigService.insertQmsBerrConfig(qmsBerrConfig));
    }

    /**
     * 修改鹤位屏配置
     */
    @PreAuthorize("@ss.hasPermi('queue:berrconfig:edit')")
    @Log(title = "鹤位屏配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsBerrConfig qmsBerrConfig)
    {
        qmsBerrConfig.setUpdateBy(getUsername());
        return toAjax(qmsBerrConfigService.updateQmsBerrConfig(qmsBerrConfig));
    }

    /**
     * 删除鹤位屏配置
     */
    @PreAuthorize("@ss.hasPermi('queue:berrconfig:remove')")
    @Log(title = "鹤位屏配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) throws Exception {
        return toAjax(qmsBerrConfigService.deleteQmsBerrConfigByIds(ids));
    }


    /**
     * 测试发送模板
     */
    @PostMapping(value = "/testTemplate/{id}")
    public AjaxResult testTemplate(@PathVariable("id") Long id)
    {
        QmsBerrConfig qmsBerrConfig = qmsBerrConfigService.selectQmsBerrConfigById(id);
        List<BerrArea> berrAreaList = BerrUtils.parseContent(qmsBerrConfig.getContentTemplate());
        try {
            BerrUtils.sendBerr(qmsBerrConfig.getDeviceIp(), qmsBerrConfig.getDevicePort().intValue(), "{}", berrAreaList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
